import axios from 'axios'
import Vue from 'vue'
import Qs from 'qs'
// import {Message, Loading} from 'element-ui'
import {MessageBox, Message} from 'element-ui'
// import _ from 'lodash'

axios.defaults.timeout = 500000
axios.defaults.baseURL = process.env.VUE_APP_API_ROOT// 服务器
// http request 拦截器
axios.interceptors.request.use(
    config => {
        console.log(config)
        if (config.headers && config.headers.ContentType && config.headers.ContentType == 'multipart/form-data') {
            config.headers = {
                'Content-Type': 'multipart/form-data;'
            }
        } else {
            config.data = Qs.stringify(config.data)
        }
        return config
    },
    err => {
        return Promise.reject(err)
    }
)

// http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.code == 200) {
            return response
        } else if (response.data.code == 444) {
            Message.error('当前用户登录已失效,请重新登陆!', '温馨提示')
            setTimeout(() => {
                window.location.href = Vue.prototype.$page['index'] + '/login'
            }, 3000)
        } else {
            Message.error(response.data
            )
            return response
        }

    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 400:
                    break
                case 401:
                    // 没有权限
                    MessageBox.alert('您没有权限操作此资源!', '没有权限', {
                        confirmButtonText: '关闭',
                        showClose: false,
                        type: 'warning'
                    })
                    break
                case 444:
                    // token没有,token验证失败,token让串改
                    MessageBox.alert('会话信息超时,请重新登陆!', '会话超时', {
                        confirmButtonText: '跳转到登陆页面',
                        showClose: false,
                        type: 'warning',
                        // eslint-disable-next-line no-unused-vars
                        callback: _ => {
                            window.location.href = Vue.prototype.$page['index'] + '/login'
                        }
                    })
                    break
                case 404:
                    // 接口没有
                    break
                case 500:
                    // 内部出错
                    break
            }
        }
        return Promise.reject(error)
    }
)

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url, params = {}) {
    return new Promise((resolve, reject) => {
        if (url.indexOf('auth/app/open') > -1) {
            let yydm = url.split('=')[1]
            localStorage.setItem('yydm', yydm)
        }
        axios.get(url, {
            params: params
        })
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                // //(err);
                reject(err)
            })
    })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}

export function postFile(url, data = {}) {
    // new 一个FormData格式的参数
    let params = new FormData()
    params.append('file', data)
    let config = {
        headers: { // 添加请求头
            'ContentType': 'multipart/form-data'
        }
    }
    return new Promise((resolve, reject) => {
        axios.post(url, params, config).then(res => {

            resolve(res)
        }).catch(err => {

            reject(err)
        })
    })
}

/**
 * 封装delet请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function delet(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, data)
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}

